#include "noteitem.h"

NoteEditor::NoteEditor(QWidget *parent, Qt::WindowFlags f) : QMdiSubWindow(parent, f)
	{
		editor = new RichTextEdit(this);
		setWidget(editor);
		setAttribute(Qt::WA_DeleteOnClose);
		setWindowState(Qt::WindowMaximized);

		connect(editor, SIGNAL(save()), this, SLOT(save()));
	}

NoteEditor::~NoteEditor(){}

void NoteEditor::attachFile()
	{
		path = QFileInfo(windowFilePath());
		if (path.exists())
		{
			QFile content(path.absoluteFilePath());
			content.open(QIODevice::ReadOnly);
			editor->setHtml(content.readAll());
			content.close();
			show();
		}

		else
		{
			QErrorMessage error(this);
			error.showMessage(tr("File does not exist."));
		}
	}

QString NoteEditor::documentTitle() const
	{
		return editor->documentTitle();
	}

void NoteEditor::save()
	{
		if (path.exists())
		{
			QFile content(path.absoluteFilePath());
			QString html = editor->toHtml();
			content.open(QIODevice::WriteOnly);
			content.write(html.toUtf8());
			content.close();
		}

		else
		{
			QErrorMessage error(this);
			error.showMessage(tr("Unable to save contents: note does not exist."));
		}
	}

void NoteEditor::setDocumentTitle(const QString &title)
	{
		editor->setDocumentTitle(title);
		setWindowTitle(title);
	}
